package com.yolib.sanyi.pic;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.yolib.sanyi.tool.YoliBLog;


import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;

public class ImageStore {

	private static ExecutorService executorService = Executors.newFixedThreadPool(2);

	public static void putBitmap(final Context context, final String key, final Bitmap bitmap, final Handler handler) {
		executorService.submit(new Runnable() {

			@Override
			public void run() {
				YoliBLog.e("Store start");
				try {
					
					File outputfile = new File(context.getExternalFilesDir(null), key);
					BufferedOutputStream bos = new BufferedOutputStream
					(new FileOutputStream(outputfile));
					boolean saved = bitmap.compress(Bitmap.CompressFormat.JPEG, 30, bos);
					bos.flush();
					bos.close();
					
					YoliBLog.e("FILE SAVE HERE: "+outputfile.getPath());
					if(handler != null)
						handler.sendEmptyMessage(0);
					if(saved){
//						synchronized(sFileCache){
//							sFileCache.put(key, getFile(key).length());
//						}
					}
				} catch (IOException e1) {
					YoliBLog.e("File SAVE ERROR");
					e1.printStackTrace();
				}
//				object.recycle();
//				System.gc();
//				if(mHandler != null)
//				{
//					Message msg = Message.obtain(mHandler);
//					Bundle mBundle = new Bundle();
//					mBundle.putString("src", id);
//					msg.setData(mBundle);
//					mHandler.sendMessage(msg);
//					YoliBLog.e("Store finish");
//				}
			}
		});
	}
}
